package com.ubxtech.jdk21.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ubxtech.cache.redis.RedisManager;
import com.ubxtech.core.context.ContextToken;
import com.ubxtech.core.context.Result;
import com.ubxtech.core.plugins.Page;
import com.ubxtech.core.req.BatchIdReq;
import com.ubxtech.core.req.IdReq;
import com.ubxtech.core.req.PageReq;
import com.ubxtech.core.validation.ValidationGroups;
import com.ubxtech.db.util.PageBuilderUtil;
import com.ubxtech.jdk21.entity.BasicSlaveAreaLink;
import com.ubxtech.jdk21.req.BasicSlaveAreaLinkReq;
import com.ubxtech.jdk21.service.BasicSlaveAreaLinkService;
import com.ubxtech.jdk21.vo.BasicSlaveAreaLinkVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 区域Link表(各区域间关系，例；楼栋包含楼层)Controller
 *
 * @author victor.xiao
 * @date 2024-03-26 20:50
 */
@RestController
@RequestMapping("/basicSlaveAreaLink")
@Slf4j
@Tag(name = "区域Link表(各区域间关系，例；楼栋包含楼层)控制器")
public class BasicSlaveAreaLinkController {
    @Resource
    private BasicSlaveAreaLinkService basicSlaveAreaLinkService;
    @Resource
    private RedisManager redisManager;

    @Operation(summary = "分页列表",description = "des")
    @PostMapping("/page")
    public Result<Page<BasicSlaveAreaLinkVo>> page(@Validated(value = {ValidationGroups.Page.class}) @RequestBody PageReq<BasicSlaveAreaLinkReq> pageReq){
        IPage<BasicSlaveAreaLink> page = PageBuilderUtil.buildPage(BasicSlaveAreaLink.class,pageReq);
        QueryWrapper<BasicSlaveAreaLink> queryWrapper = new QueryWrapper<>();
        basicSlaveAreaLinkService.page(page,queryWrapper);
        return Result.ok(PageBuilderUtil.toLocalPage(page, BasicSlaveAreaLinkVo.class));
    }

    // get 对象参数 添加@ParameterObject   或者springdoc.default-flat-param-object=true就能解析对象
    @Operation(summary = "根据ID查询区域Link表(各区域间关系，例；楼栋包含楼层)详情",description = "des11")
    @GetMapping("/detail")
    public Result<BasicSlaveAreaLinkVo> detail(@Validated(value = ValidationGroups.Detail.class) IdReq idReq) {
        BasicSlaveAreaLink entity = basicSlaveAreaLinkService.getById(idReq.getId());
        if (entity == null) {
            return Result.ok();
        }
        BasicSlaveAreaLinkVo vo = BeanUtil.copyProperties(entity, BasicSlaveAreaLinkVo.class);
        ContextToken redis = redisManager.get("cml:master:user:id:1706258624112570369:9");
        log.info("redis============={}",redis);
        return Result.ok(vo);
    }


    @Operation(summary = "新增区域Link表(各区域间关系，例；楼栋包含楼层",description = "新增区域Link表(各区域间关系，例；楼栋包含楼层")
    @PostMapping("/add")
    public Result<BasicSlaveAreaLinkVo> add(@Validated(value = ValidationGroups.Add.class) @RequestBody BasicSlaveAreaLinkReq req) {
        BasicSlaveAreaLink entity = BeanUtil.copyProperties(req, BasicSlaveAreaLink.class);
        entity.setVersion(null);
        basicSlaveAreaLinkService.save(entity);
        BasicSlaveAreaLinkVo vo = BeanUtil.copyProperties(entity, BasicSlaveAreaLinkVo.class);
        return Result.ok(vo);
    }

    @Operation(summary = "修改区域Link表(各区域间关系，例；楼栋包含楼层)")
    @PostMapping("/update")
    public Result<Void> update(@Validated(value = ValidationGroups.Update.class) @RequestBody BasicSlaveAreaLinkReq req) {
        BasicSlaveAreaLink entity = BeanUtil.copyProperties(req, BasicSlaveAreaLink.class);
        basicSlaveAreaLinkService.updateById(entity);
        return Result.ok();
    }

    @Operation(summary = "删除区域Link表(各区域间关系，例；楼栋包含楼层)")
    @PostMapping("/delete")
    public Result<Void> delete(@Validated(value = ValidationGroups.Delete.class) @RequestBody BatchIdReq batchIdReq) {
        basicSlaveAreaLinkService.removeBatchByIds(batchIdReq.getIds());
        return Result.ok();
    }
}